#include <stdio.h>
#include <omp.h>

#define NUM_STEPS 100000
#define THREAD_NUM 10
#define PERTHREAD_STEPS NUM_STEPS/THREAD_NUM

static long num_steps = NUM_STEPS;
double step;

int main(int argc , char ** argv ){

	int i;

	double x,pi,sum=0.0;

	printf("wtime :%f\n" , omp_get_wtime() );
	double sub_sums[THREAD_NUM];
	for(i=0;i<THREAD_NUM;i++){
		sub_sums[i]=0.0;
	}

	step = 1.0/(double)num_steps;

	omp_set_num_threads(THREAD_NUM);

	#pragma omp parallel
	{
		int id=omp_get_thread_num();

//		printf("thread id is %d\n" , id);

		for( i=0 ; i<PERTHREAD_STEPS ;i++){

			x=(id*PERTHREAD_STEPS + i + 0.5)*step;

			sub_sums[id] += 4.0/(1.0+x*x);
		}
	}

	pi=0.0;
	for(i=0;i<THREAD_NUM;i++){
		pi += sub_sums[i] * step;
	}
	printf("wtime :%f\n" , omp_get_wtime() );

	printf("pi:%f\n",pi);
}
